/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RondeTab.java
 *
 * Created on 9-jan-2012, 12:16:09
 */
package view;

import components.Database;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import model.Ronde;
import model.Speler;
import model.Toernooi;
import model.ToernooiInschrijving;

/**
 *
 * @author tomkeim
 */
public class RondeTab extends javax.swing.JPanel {
    
    Toernooi selectedToernooi = null;
    DefaultListModel listModel;
    Ronde selectedRonde;
    Ronde[] rondes;
    ToernooiInschrijving[] inschrijvingen;
    DefaultTableModel tableModel;

    /** Creates new form RondeTab */
    public RondeTab() {

        if (!Database.openConnection("meru.hhs.nl", "db11031425", "usr11031425", "Dedaikahsh")) {

            JOptionPane.showMessageDialog(null, "De verbinding met de database is mislukt. Probeer het later opnieuw.");
            System.exit(0);
        }


        tableModel = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int rowIndex, int mColIndex) {
                return false;
            }
        };

        tableModel.addColumn("#");
        tableModel.addColumn("Speler naam");
        tableModel.addColumn("Speler rating");

        initComponents();

        tafelTable.setModel(tableModel);
        Toernooi[] toernooien = Toernooi.getToernooien();



        for (int i = 0; i < toernooien.length; i++) {
            toernooiBox.addItem(toernooien[i]);
        }
        
        disableAll();

    }
    public void disableAll() {
        winnaar1Box.setEnabled(false);
        winnaar2Box.setEnabled(false);
        winnaar3Box.setEnabled(false);
        eindeRondeButton.setEnabled(false);
        tafelTable.setEnabled(false);
        inleg.setText("");
        inleg.setEnabled(false);
    }
    
    public void enableAll() {
        winnaar1Box.setEnabled(true);
        winnaar2Box.setEnabled(true);
        winnaar3Box.setEnabled(true);
        eindeRondeButton.setEnabled(true);
        tafelTable.setEnabled(true);
        inleg.setText("");
        inleg.setEnabled(true);
    }
    
    public void clearTable() {
        tableModel.setRowCount(0);
    }
    
    public void fillTable() {
        clearTable();
        winnaar1Box.removeAllItems();
        winnaar2Box.removeAllItems();
        winnaar3Box.removeAllItems();
        if (inschrijvingen.length > 0) {
            for (int i = 0; i < inschrijvingen.length; i++) {
                ToernooiInschrijving inschrijving = inschrijvingen[i];
                Speler speler = inschrijving.getSpeler();
                tableModel.addRow(new Object[]{(i + 1), speler.getNaam(), speler.getRating()});
                winnaar1Box.addItem(speler);
                winnaar2Box.addItem(speler);
                winnaar3Box.addItem(speler);

            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        toernooiBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        rondesList = new javax.swing.JList();
        selectRonde = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tafelTable = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        winnaar2Box = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        winnaar1Box = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        winnaar3Box = new javax.swing.JComboBox();
        eindeRondeButton = new javax.swing.JButton();
        newRondeButton = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        inleg = new javax.swing.JTextField();

        toernooiBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toernooiBoxActionPerformed(evt);
            }
        });

        jLabel1.setText("Selecteer toernooi:");

        jScrollPane1.setViewportView(rondesList);

        selectRonde.setText("Selecteer ronde:");

        tafelTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tafelTable);

        jLabel2.setText("Tafelindeling:");

        jLabel3.setText("Winnaars");

        jLabel4.setText("1.:");

        jLabel5.setText("2.:");

        winnaar1Box.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                winnaar1BoxActionPerformed(evt);
            }
        });

        jLabel6.setText("3.:");

        winnaar3Box.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                winnaar3BoxActionPerformed(evt);
            }
        });

        eindeRondeButton.setText("Einde ronde");
        eindeRondeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eindeRondeButtonActionPerformed(evt);
            }
        });

        newRondeButton.setText("Nieuwe ronde");
        newRondeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newRondeButtonActionPerformed(evt);
            }
        });

        jLabel7.setText("Inleg:");
        jLabel7.setToolTipText("De inleg in het aantal rating punten");

        inleg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inlegActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 720, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(newRondeButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 139, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(layout.createSequentialGroup()
                                        .add(selectRonde)
                                        .add(36, 36, 36))
                                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 147, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .add(18, 18, 18)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(layout.createSequentialGroup()
                                        .add(jLabel2)
                                        .add(330, 330, 330))
                                    .add(layout.createSequentialGroup()
                                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 402, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel3)
                                    .add(layout.createSequentialGroup()
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(layout.createSequentialGroup()
                                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                                    .add(jLabel6)
                                                    .add(jLabel5)
                                                    .add(jLabel4))
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                                    .add(winnaar1Box, 0, 125, Short.MAX_VALUE)
                                                    .add(org.jdesktop.layout.GroupLayout.LEADING, winnaar2Box, 0, 0, Short.MAX_VALUE)
                                                    .add(org.jdesktop.layout.GroupLayout.LEADING, winnaar3Box, 0, 0, Short.MAX_VALUE)))
                                            .add(layout.createSequentialGroup()
                                                .add(jLabel7)
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                .add(inleg, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE))))))
                            .add(layout.createSequentialGroup()
                                .add(jLabel1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(toernooiBox, 0, 618, Short.MAX_VALUE))))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(604, 604, 604)
                        .add(eindeRondeButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(toernooiBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(selectRonde)
                    .add(jLabel2)
                    .add(jLabel3))
                .add(11, 11, 11)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(7, 7, 7)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel7)
                            .add(inleg, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(12, 12, 12)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(winnaar1Box, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel4))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(winnaar2Box, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel5))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(winnaar3Box, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel6))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(eindeRondeButton))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 217, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 17, Short.MAX_VALUE)
                        .add(newRondeButton)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    public void refreshAll() {
     
        selectedRonde = null;
        rondes = Ronde.getRondes(this.selectedToernooi);
        //System.out.println(rondes);
        rondesList.setListData(rondes);
        rondesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        rondesList.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent le) {
                int idx = rondesList.getSelectedIndex();
                if (idx != -1) {
                    selectedRonde = (Ronde) rondes[idx];

                    if (selectedRonde.getStatus() == 1) {
                        //Ronde is al gespeeld.
                        JOptionPane.showMessageDialog(null, "Deze ronde is al gespeeld. Selecteer een andere");
                        rondesList.clearSelection();
                        selectedRonde = null;
                        disableAll();
                        clearTable();
                    } else {
                        fillTable();
                        enableAll();
                    }
                }
            }
        });
    }
    private void toernooiBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toernooiBoxActionPerformed
        selectedToernooi = (Toernooi) toernooiBox.getSelectedItem();
        inschrijvingen = ToernooiInschrijving.getToernooiInschrijvingen(selectedToernooi,true);
        
        refreshAll();
    }//GEN-LAST:event_toernooiBoxActionPerformed

    private void winnaar3BoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_winnaar3BoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_winnaar3BoxActionPerformed

    private void newRondeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newRondeButtonActionPerformed
        Ronde ronde = new Ronde();
        ronde.setRondeNummer((rondes.length + 1));
        ronde.setToernooi(selectedToernooi);
        ronde.insert();
        
        refreshAll();
    }//GEN-LAST:event_newRondeButtonActionPerformed

    private void eindeRondeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eindeRondeButtonActionPerformed
        try{
        if (selectedRonde != null) {
            selectedRonde.setStatus(1);
            selectedRonde.update();
            
            Speler winnaar1 = (Speler)winnaar1Box.getSelectedItem();
            Speler winnaar2 = (Speler)winnaar2Box.getSelectedItem();
            Speler winnaar3 = (Speler)winnaar3Box.getSelectedItem();
            if(winnaar1.equals(winnaar2) || winnaar1.equals(winnaar3) ||
                winnaar2.equals(winnaar1) || winnaar2.equals(winnaar3) ||
                winnaar3.equals(winnaar1) || winnaar3.equals(winnaar2)) {
                
                JOptionPane.showMessageDialog(null,"U dient 3 apparte spelers te selecteren");
            } else {
            double inlegDouble = Double.parseDouble(inleg.getText());
            double pot = inschrijvingen.length * inlegDouble;
            for(int i = 0; i< inschrijvingen.length; i++) {
                ToernooiInschrijving inschrijving = inschrijvingen[i];
                    Speler speler = inschrijving.getSpeler();
                    speler.lowerRating(inlegDouble);
                    speler.update();
            }
            
            double winst1 = pot * 0.6;
            double winst2 = pot * 0.3;
            double winst3 = pot * 0.1;
            
            double oudbedrag1 = winnaar1.getRating();
            double oudbedrag2 = winnaar2.getRating();
            double oudbedrag3 = winnaar3.getRating();
            
            winnaar1.higherRating(winst1);
            winnaar2.higherRating(winst2);
            winnaar3.higherRating(winst3);
            winnaar1.update();
            winnaar2.update();
            winnaar3.update();
            clearTable();
            refreshAll();
            disableAll();
            JOptionPane.showMessageDialog(null, "De ronde is gespeeld. De resultaten zijn"
                    + "\n nr 1: " + winnaar1.getNaam() + " had " + oudbedrag1 + " en heeft  nu " + winst1 + " gewonnen en heeft nu " + winnaar1.getRating()
                    + "\n nr 2: " + winnaar2.getNaam() + " had " + oudbedrag2 + " en heeft  nu " + winst2 + " gewonnen en heeft nu " + winnaar2.getRating()
                    + "\n nr 3: " + winnaar3.getNaam() + " had " + oudbedrag3 + " en heeft  nu " + winst3 + " gewonnen en heeft nu " + winnaar3.getRating());
        } } else {
            JOptionPane.showMessageDialog(null, "U dient eerst een ronde te selecteren");
        }
        }
        catch(NumberFormatException e){
            JOptionPane.showMessageDialog(null, "U mag geen velden leeg laten");
        }
       
    }//GEN-LAST:event_eindeRondeButtonActionPerformed

private void inlegActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inlegActionPerformed
// TODO add your handling code here:
    
}//GEN-LAST:event_inlegActionPerformed

private void winnaar1BoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_winnaar1BoxActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_winnaar1BoxActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton eindeRondeButton;
    private javax.swing.JTextField inleg;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton newRondeButton;
    private javax.swing.JList rondesList;
    private javax.swing.JLabel selectRonde;
    private javax.swing.JTable tafelTable;
    private javax.swing.JComboBox toernooiBox;
    private javax.swing.JComboBox winnaar1Box;
    private javax.swing.JComboBox winnaar2Box;
    private javax.swing.JComboBox winnaar3Box;
    // End of variables declaration//GEN-END:variables
}
